package com.example.libo.huihui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

import com.example.libo.huihui.R;
import com.example.libo.huihui.adapter.GuoneiAdapter;
import com.example.libo.huihui.application.MyApplication;
import com.example.libo.huihui.been.GuoneiListEntity;
import com.example.libo.huihui.been.GuoneiTopEntity;
import com.example.libo.huihui.utils.AnimUtils;

import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GuoFragment extends Fragment implements Callback<GuoneiListEntity>{
    private RecyclerView recyclerView;
    private ArrayList datas = new ArrayList();
    private GuoneiAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guo, container, false);
        initView(view);
        loadAnimation(view);
        return view;
    }

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_guonei);
        adapter = new GuoneiAdapter(getActivity(),datas);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),2);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int spanSize = 1;
                switch (adapter.getItemViewType(position)){
                    case 1:
                        spanSize = 1;
                        break;
                    case 2:
                        spanSize = 2;
                        break;
                    default:
                        spanSize = 2;
                        break;
                }
                return spanSize;
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
        MyApplication.getHttpService().getGuoneilist().enqueue(this);
        MyApplication.getHttpService().getGuoneiTop().enqueue(new Callback<GuoneiTopEntity>() {
            @Override
            public void onResponse(Call<GuoneiTopEntity> call, Response<GuoneiTopEntity> response) {

            }

            @Override
            public void onFailure(Call<GuoneiTopEntity> call, Throwable t) {

            }
        });
    }

    private void loadAnimation(View view){
        final TextView tvTip = (TextView) view.findViewById(R.id.tv_tip);
        AnimUtils.appearAnim(tvTip);
    }

    @Override
    public void onResponse(Call<GuoneiListEntity> call, Response<GuoneiListEntity> response) {
        datas.addAll(response.body().getData().getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<GuoneiListEntity> call, Throwable t) {

    }
}